﻿/* 
* Copyright (c) 2010, The University of Texas at Austin
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, 
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, 
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, 
* this list of conditions and the following disclaimer in the documentation and/or other materials 
* provided with the distribution.
*
* Neither the name of The University of Texas at Austin nor the names of its contributors may be 
* used to endorse or promote products derived from this software without specific prior written 
* permission.
* 
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR 
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS 
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Xml.Linq;
using Bio.Data.Interfaces.Drawing;
using Bio.Data.Providers.Structure.XRNA.Internal;

namespace Bio.Data.Providers.Structure
{
    public class RNASecondaryStructureBasePairConnector : IBasePairConnectorShape
    {
        internal static RNASecondaryStructureBasePairConnector FromXRNALineShape(XRNALineShape xrnaLine)
        {
            if (xrnaLine == null)
                return null;

            RNASecondaryStructureBasePairConnector retValue = new RNASecondaryStructureBasePairConnector();
            retValue.Size = xrnaLine.Thickness;
            retValue.StrokeColor = xrnaLine.Color;
            return retValue;
        }

        internal static RNASecondaryStructureBasePairConnector FromXRNACircleShape(XRNACircleShape xrnaCircle)
        {
            if (xrnaCircle == null)
                return null;

            RNASecondaryStructureBasePairConnector retValue = new RNASecondaryStructureBasePairConnector();
            retValue.Size = xrnaCircle.Diameter;
            retValue.EdgeThickness = xrnaCircle.EdgeThickness;
            retValue.Filled = xrnaCircle.Filled;
            retValue.FillColor = xrnaCircle.FillColor;
            retValue.StrokeColor = xrnaCircle.EdgeColor;
            return retValue;
        }

        internal static RNASecondaryStructureBasePairConnector FromRNAML(IEnumerable<XComment> formattingcomments)
        {
            RNASecondaryStructureBasePairConnector retValue = null;
            foreach (var bpconnformattingcomment in formattingcomments)
            {
                if (!string.IsNullOrEmpty(bpconnformattingcomment.Value) && bpconnformattingcomment.Value.StartsWith(RNAMLTags.StrMLExtension))
                {
                    string[] fields = Regex.Split(bpconnformattingcomment.Value, "::");
                    if (fields.Length == 3 && fields[0].Equals(RNAMLTags.StrMLExtension))
                    {
                        if (fields[1].Equals(RNAStructureModelXMLTags.StructureModelPairConnectorFillColorLabel))
                        {
                            if (retValue == null)
                                retValue = new RNASecondaryStructureBasePairConnector();

                            retValue.FillColor = fields[2];
                        }
                        else if (fields[1].Equals(RNAStructureModelXMLTags.StructureModelPairConnectorStrokeColorLabel))
                        {
                            if (retValue == null)
                                retValue = new RNASecondaryStructureBasePairConnector();

                            retValue.StrokeColor = fields[2];
                        }
                        else if (fields[1].Equals(RNAStructureModelXMLTags.StructureModelPairConnectorSizeLabel))
                        {
                            if (retValue == null)
                                retValue = new RNASecondaryStructureBasePairConnector();

                            double sizeVal;
                            if (double.TryParse(fields[2], out sizeVal))
                            {
                                if (sizeVal > 0.0)
                                    retValue.Size = sizeVal;
                            }
                        }
                        else if (fields[1].Equals(RNAStructureModelXMLTags.StructureModelPairConnectorStrokeThicknessLabel))
                        {
                            if (retValue == null)
                                retValue = new RNASecondaryStructureBasePairConnector();

                            double thicknessVal;
                            if (double.TryParse(fields[2], out thicknessVal))
                            {
                                if (thicknessVal > 0.0)
                                    retValue.EdgeThickness = thicknessVal;
                            }
                        }
                        else if (fields[1].Equals(RNAStructureModelXMLTags.StructureModelPairConnectorFilledLabel))
                        {
                            if (retValue == null)
                                retValue = new RNASecondaryStructureBasePairConnector();

                            bool filledVal;
                            if (bool.TryParse(fields[2], out filledVal))
                            {
                                retValue.Filled = filledVal;
                            }
                        }
                    }
                }
            }
            return retValue;
        }

        public static RNASecondaryStructureBasePairConnector FromXML(XElement props)
        {
            if (props != null &&
                !props.IsEmpty)
            {
                RNASecondaryStructureBasePairConnector retValue = null;
                XElement fillColor = props.Element(RNAStructureModelXMLTags.StructureModelPairConnectorFillColorLabel);
                XElement strokeColor = props.Element(RNAStructureModelXMLTags.StructureModelPairConnectorStrokeColorLabel);
                XElement filledState = props.Element(RNAStructureModelXMLTags.StructureModelPairConnectorFilledLabel);
                XElement size = props.Element(RNAStructureModelXMLTags.StructureModelPairConnectorSizeLabel);
                XElement strokeThickness = props.Element(RNAStructureModelXMLTags.StructureModelPairConnectorStrokeThicknessLabel);

                double sizeVal;
                if (size != null &&
                    !size.IsEmpty &&
                    double.TryParse(size.Value, out sizeVal))
                {
                    if (retValue == null)
                        retValue = new RNASecondaryStructureBasePairConnector();

                    if (sizeVal > 0.0)
                        retValue.Size = sizeVal;
                }

                double strokeThicknessVal;
                if (strokeThickness != null &&
                    !strokeThickness.IsEmpty &&
                    double.TryParse(strokeThickness.Value, out strokeThicknessVal))
                {
                    if (retValue == null)
                        retValue = new RNASecondaryStructureBasePairConnector();

                    if (strokeThicknessVal > 0.0)
                        retValue.EdgeThickness = strokeThicknessVal;
                }

                if (fillColor != null &&
                    !fillColor.IsEmpty)
                {
                    if (retValue == null)
                        retValue = new RNASecondaryStructureBasePairConnector();

                    retValue.FillColor = fillColor.Value;
                }

                if (strokeColor != null &&
                    !strokeColor.IsEmpty)
                {
                    if (retValue == null)
                        retValue = new RNASecondaryStructureBasePairConnector();

                    retValue.StrokeColor = strokeColor.Value;
                }

                bool filledVal;
                if (filledState != null &&
                    !filledState.IsEmpty &&
                    bool.TryParse(filledState.Value, out filledVal))
                {
                    if (retValue == null)
                        retValue = new RNASecondaryStructureBasePairConnector();

                    retValue.Filled = filledVal;
                }

                return retValue;
            }
            else { return null; }
        }

        #region IBasePairConnectorShape Members

        public string FillColor { get; set; }
        public string StrokeColor { get; set; }
        public bool? Filled { get; set; }
        public double EdgeThickness { get; set; }
        public double Size { get; set; }

        #endregion
    }
}
